package com.selfdriving.activity;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.selfdriving.BaseFragmentActivity;
import com.selfdriving.R;
import com.selfdriving.adapter.DiscussAdapter;
import com.selfdriving.bean.Discuss;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.MyArticleDetails;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.SystemSettings;
import com.selfdriving.view.CircularImage;

public class AboutMeMyArticleDetails extends BaseFragmentActivity implements
		OnClickListener
{

	private ImageView img0ImgeView;// 大图
	private ImageView img1ImgeView;
	private ImageView img2ImgeView;
	private TextView text;// 信息描述
	private TextView time;
	private TextView laudNum;
	private TextView name;
	private Button examineBut;
	private int position;
	private boolean flag;
	private TextView title;
	private ImageButton imgBreak;
	private MyArticleDetails myArticleDetails;
	private CircularImage image;
	private String imgs[];
	private Button submitDetails;
	private RatingBar ratingBar;
	private int starNum = 1;
	private EditText content;
	private ListView listView;
	BaseAdapter adapter;
	public List<Entity> listDiscuss = new ArrayList<Entity>();
	int page = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_aboutme_my_article_details);
		mApplication.intFlag = 1;
		initViews();
		initEvents();
		adapter = new DiscussAdapter(mApplication, this, listDiscuss);
		getDiscuss();
	}

	@Override
	protected void initViews()
	{
		listView = (ListView) findViewById(R.id.comment);
		content = (EditText) findViewById(R.id.input_details);
		ratingBar = (RatingBar) super.findViewById(R.id.discuss_ratingbar);
		submitDetails = (Button) super.findViewById(R.id.submitDetails);
		image = (CircularImage) super.findViewById(R.id.ivIcon);
		imgBreak = (ImageButton) super.findViewById(R.id.head_break);
		imgBreak.setVisibility(View.VISIBLE);
		examineBut = (Button) super.findViewById(R.id.examine_but);
		img0ImgeView = (ImageView) super.findViewById(R.id.examine_img);
		img1ImgeView = (ImageView) super.findViewById(R.id.examine_img1);
		img2ImgeView = (ImageView) super.findViewById(R.id.examine_img2);
		text = (TextView) super.findViewById(R.id.examine_text);
		time = (TextView) super.findViewById(R.id.examine_time);
		title = (TextView) super.findViewById(R.id.top_text_middle);
		name = (TextView) super.findViewById(R.id.examine_name);
		laudNum = (TextView) super.findViewById(R.id.examine_num);
		Intent intent = super.getIntent();
		flag = intent.getBooleanExtra("flag", false);
		position = intent.getIntExtra("position", 0);
		myArticleDetails = mApplication.myArticleDetails;
		if (mApplication.user == null
				|| !SystemSettings.getBoolean(this,
						"laud" + mApplication.user.getPhone()
								+ myArticleDetails.getId()))
		{
			examineBut.setSelected(true);
			examineBut.setEnabled(false);
		}
		imageLoader.displayImage(Constant.POST1 + myArticleDetails.getUid(),
				image, options);
		name.setText(myArticleDetails.getUname());
		title.setText(myArticleDetails.getTitle());
		if (myArticleDetails.getContent().length() > 0)
		{
			text.setVisibility(View.VISIBLE);
			text.setText(myArticleDetails.getContent());
		}
		time.setText(myArticleDetails.getTimes());
		laudNum.setText(myArticleDetails.getLaud());
		String img0 = myArticleDetails.getImg0();
		String img1 = myArticleDetails.getImg1();
		String img2 = myArticleDetails.getImg2();
		if (img0 != null)
		{
			imgs = new String[1];
			imgs[0] = myArticleDetails.getImg0();
			img0ImgeView.setVisibility(View.VISIBLE);
			imageLoader.displayImage(myArticleDetails.getImg0(), img0ImgeView,
					options);
		}
		if (img1 != null)
		{
			imgs = new String[2];
			imgs[0] = myArticleDetails.getImg0();
			imgs[1] = myArticleDetails.getImg1();
			img1ImgeView.setVisibility(View.VISIBLE);
			imageLoader.displayImage(myArticleDetails.getImg1(), img1ImgeView,
					options);
		}
		if (img2 != null)
		{
			imgs = new String[3];
			imgs[0] = myArticleDetails.getImg0();
			imgs[1] = myArticleDetails.getImg1();
			imgs[2] = myArticleDetails.getImg2();
			img2ImgeView.setVisibility(View.VISIBLE);
			imageLoader.displayImage(myArticleDetails.getImg2(), img2ImgeView,
					options);
		}
		mApplication.viewImg = imgs;
	}

	@Override
	protected void initEvents()
	{
		imgBreak.setOnClickListener(this);
		submitDetails.setOnClickListener(this);
		ratingBar
				.setOnRatingBarChangeListener(new OnRatingBarChangeListenerImpl());
	}

	private class OnRatingBarChangeListenerImpl implements
			RatingBar.OnRatingBarChangeListener
	{
		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating,
				boolean fromUser)
		{
			starNum = (int) rating; // 取得当前值
		}
	}

	Handler handler = new Handler()
	{
		public void handleMessage(android.os.Message msg)
		{
			switch (msg.what)
			{
			case -1:
				if (mApplication.intFlag < 5)
				{
					page = 1;
					getDiscuss();
					mApplication.intFlag++;
				}
				break;
			}
		};
	};

	public void getDiscuss()
	{
		AjaxParams params = new AjaxParams();
		params.put("f", "20");
		params.put("id", myArticleDetails.getId());
		params.put("page", "" + page);
		System.out.println(Constant.POST + "&f=20&id="
				+ myArticleDetails.getId() + "&page=" + page);
		FinalHttp finalHttp = new FinalHttp();
		finalHttp.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				// TODO Auto-generated method stub
				super.onFailure(t, strMsg);
				handler.sendEmptyMessageAtTime(-1, 1000);
			}

			@Override
			public void onSuccess(Object t)
			{
				// TODO Auto-generated method stub
				super.onSuccess(t);
				try
				{
					if (t.toString().contains("empty"))
					{
						showShortToast("暂无评论信息");
						return;
					}
					System.out.println(t.toString());
					JSONArray array = new JSONArray(t.toString());
					Discuss discuss = null;
					JSONObject object = null;
					for (int i = 0; i < array.length(); i++)
					{
						object = array.getJSONObject(i);
						String id = object.getString("id");
						String times = object.getString("times");
						String content = object.getString("content");
						String user = object.getString("user");
						String star = object.getString("star");
						discuss = new Discuss(id, user, times, content, star);
						listDiscuss.add(discuss);
					}
					page++;
				} catch (JSONException e)
				{
					handler.sendEmptyMessageAtTime(-1, 1000);
					e.printStackTrace();
				}
				if (adapter == null)
				{
					adapter = new DiscussAdapter(mApplication,
							AboutMeMyArticleDetails.this, listDiscuss);
					listView.setAdapter(adapter);
				} else
				{
					adapter.notifyDataSetChanged();
				}
			}
		});
	}

	public void postDiscuss()
	{
		String cont = content.getText().toString().trim();
		if (mApplication.user == null)
		{
			showShortToast("请登录后再进行评论");
			return;
		}
		if (cont.length() < 1)
		{
			showShortToast("请输入评论内容");
			return;
		}
		showLoadingDialog("评论发布中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "24");
		params.put("id", myArticleDetails.getId());
		params.put("star", "" + starNum);
		params.put("phone", mApplication.user.getPhone());
		params.put("txt", cont);
		FinalHttp finalHttp = new FinalHttp();
		finalHttp.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				// TODO Auto-generated method stub
				super.onFailure(t, strMsg);
				showShortToast("评论失败");
				dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object t)
			{
				// TODO Auto-generated method stub
				super.onSuccess(t);
				if (t.toString().contains("000000"))
				{
					showShortToast("评论提交成功");
				}
				dismissLoadingDialog();
			}
		});
	}

	@Override
	public void onBackPressed()
	{
		finish();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.submitDetails:
			postDiscuss();
			break;
		case R.id.head_break:
			finish();
			break;
		}
	}
}
